﻿/// <reference path="application.js" />

function Sprite(imageURL) {
    /// in world coordinates
    this.x = 0;
    this.y = 0;

    /// relative center
    this.centerX = 0;
    this.centerY = 0;

    /// size of the image source sprite to extract
    this.sourceW = 0;
    this.sourceH = 0;

    this.rowIndex = 0;
    this.rowLenght = new Array(0);
    this.cellIndex = 0;
    this.image = new Image();
    this.image.src = imageURL;
    this.scaleX = 1;
    this.scaleY = 1;

    if (typeof Sprite.initialized == "undefined") {
        Sprite.prototype.Draw = function () {

            // Draw sprite
            window.app.context.container2d.save();
            window.app.context.container2d.translate(app.UI.ToScreenX(this.x), app.UI.ToScreenY(this.y));
            window.app.context.container2d.scale(this.scaleX, this.scaleY);
            window.app.context.container2d.drawImage(
                this.image,
                this.cellIndex * this.sourceW, this.rowIndex * this.sourceH, this.sourceW, this.sourceH,
                -this.centerX, -this.centerY, this.sourceW, this.sourceH);
            window.app.context.container2d.restore();

            // Draw debug info
            if (app.context.isInDebugMode) {

                // center Cross
                window.app.context.container2d.fillStyle = "red";
                window.app.context.container2d.strokeStyle = "rgba(0,0,0,0.1)";
                window.app.context.container2d.lineWidth = 1;
                window.app.context.container2d.fillRect(app.UI.ToScreenX(this.x - 4), app.UI.ToScreenY(this.y + 1), 8, 2);
                window.app.context.container2d.fillRect(app.UI.ToScreenX(this.x - 1), app.UI.ToScreenY(this.y + 4), 2, 8);
                //window.app.UI.FillRect(this.x - 4, this.y - 1, 8, 2);
                //window.app.UI.FillRect(this.x - 1, this.y - 4, 2, 8);

                // Boundary
                window.app.context.container2d.strokeRect(
                    app.UI.ToScreenX(this.x - this.centerX - 0.5),
                    app.UI.ToScreenY(this.y - this.centerY - 0.5) - this.sourceH + 1,
                    this.sourceW + 1, 
                    this.sourceH + 1);
                //window.app.UI.StrokeRect(this.x - this.centerX - 0.5, this.y - this.centerY - 0.5, this.sourceW + 1, this.sourceH + 1);

                // coordinates
                window.app.context.container2d.font = "9px arial";
                window.app.context.container2d.textAlign = "left";
                window.app.context.container2d.fillStyle = "#333333";
                app.context.container2d.fillText("[" + this.x + " ; " + this.y + "]", app.UI.ToScreenX(this.x - this.centerX), app.UI.ToScreenY(this.y + this.centerY + 2));
                //window.app.UI.FillText("[" + this.x + " ; " + this.y + "]", this.x - this.centerX, this.y + this.centerY + 2);
            }
        };

        /// Increases the
        Sprite.prototype.StepForward = function (loop) {
            if (loop == undefined) { loop = true; }
            if (this.cellIndex < this.rowLenght[this.rowIndex] - 1) {
                this.cellIndex++;
            } else if(loop) {
                this.cellIndex = 0;
            }
        };

        Sprite.prototype.StepBackward = function (loop) {
            if (loop == undefined) { loop = true; }
            if (this.cellIndex > 0) {
                this.cellIndex--;
            } else if(loop){
                this.cellIndex = this.rowLenght[this.rowIndex] - 1;
            }
        };

        Sprite.prototype.Reset = function () {
            this.cellIndex = 0;
        };


        Sprite.initialized = true;
    }
};